package com.atguigu.mybatis.test;

import com.atguigu.mybatis.entity.Animal;
import com.atguigu.mybatis.entity.Employee;
import com.atguigu.mybatis.mapper.AnimalMapper;
import com.atguigu.mybatis.mapper.EmployeeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.*;

@Slf4j
public class DemoTest03 {

    private SqlSession session;

    @BeforeEach
    public void init() throws IOException {
        session = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis-config.xml"))
                .openSession();
    }

    @AfterEach
    public void clear() {
        session.commit();
        session.close();
    }

    @Test
    public void test01HelloWorld() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmployee(2);
        log.debug("employee = " + employee);
    }

    @Test
    public void test02Logger() {
        log.trace("hello trace");
        log.debug("hello debug");
        log.info("hello info");
        log.warn("hello warn");
        log.error("hello error");
    }

    @Test
    public void test03Dollar() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Integer empId = 3;
        employeeMapper.deleteById(empId);
    }

    @Test
    public void test04SaveEmployee() {

        Employee employee = new Employee(4, "zhangsan", 3000d);

        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        int effectedRows = employeeMapper.insertEmployee(employee);

        log.debug("effectedRows = " + effectedRows);
    }

    @Test
    public void test05UpdateEmployee() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Integer empId = 4;
        Double empSalary = 8111d;

        employeeMapper.updateEmpSalary(empId, empSalary);
    }

    @Test
    public void test06DeleteEmployee() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Integer empId = 4;
        Double empSalary = 11111d;

        // 把 SQL 语句的参数封装到 Map 集合中
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("empId", empId);
        paramMap.put("empSalary", empSalary);

        employeeMapper.updateEmpSalaryByMap(paramMap);
    }

    @Test
    public void test07SelectAvgSalary() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Double avgSalary = employeeMapper.selectAvgSalary();

        log.debug("avgSalary = " + avgSalary);
    }

    @Test
    public void test08SelectEmpList() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        List<Employee> employeeList = employeeMapper.selectAll();
        for (Employee employee : employeeList) {
            log.debug("employee = " + employee);
        }
    }

    @Test
    public void test09SelectMaxSalaryAndMinSalary() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Map<String, Double> dataMap = employeeMapper.selectMinAndMaxSalary();

        Set<Map.Entry<String, Double>> entrySet = dataMap.entrySet();
        for (Map.Entry<String, Double> entry : entrySet) {
            String key = entry.getKey();
            Double value = entry.getValue();

            log.debug(key + " = " + value);
        }
    }

    @Test
    public void test10GeneratedPK() {
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Employee employee = new Employee();
        employee.setEmpName("wangwu");
        employee.setEmpSalary(3344.88);

        employeeMapper.insertEmployeeGeneratedPK(employee);

        // 获取自增主键
        Integer empId = employee.getEmpId();
        log.debug("empId = " + empId);
    }

    @Test
    public void test11UUID() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        log.debug("uuid = " + s);
    }

    @Test
    public void test12UUIDPk() {
        AnimalMapper animalMapper = session.getMapper(AnimalMapper.class);
        Animal animal = new Animal(null, "cat");

        animalMapper.insertAnimal(animal);

        String animalId = animal.getAnimalId();
        log.debug("animalId = " + animalId);
    }
}
